package com.rimuru.datastructure.linkedlist;

import java.util.Iterator;
import java.util.function.Consumer;

// 单向链表
public class SinglyLinkedList implements Iterable<Integer> { // 整体

    private Node head; // 头指针

    @Override
    public Iterator<Integer> iterator() {
        // 匿名内部类 -> 带名字内部类
        return new NodeIterator();
    }

    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw new IllegalArgumentException(String.format("index[%d] 不合法%n", index));
        }
        return node.value;
    }

    // 链表插入数据 先找到上一个节点，然后插入
    // 插入第一个位置时直接插入头部
    public void insert(int index, int value) {
        if (index == 0) {
            addFirst(value);
        } else {
            Node prev = findNode(index - 1); // 找到上一个节点
            if (prev == null) {
                throw new IllegalArgumentException(String.format("index[%d] 不合法%n", index));
            }
            prev.next = new Node(value, prev.next);
        }

    }

    // 删除第一个节点
    public void removeFirst() {
        if (head == null) {
            throw new IllegalArgumentException(String.format("index[%d] 不合法%n", 0));
        }
        head = head.next;
    }

    // 按索引删除
    public void remove(int index) {
        if (index == 0) {
            removeFirst();
            return;
        }
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw new IllegalArgumentException(String.format("index[%d] 不合法%n", 0));
        }
        Node removed = prev.next;
        if (removed == null) {
            // 被删除节点为空
            throw new IllegalArgumentException(String.format("index[%d] 不合法%n", index));
        }
        prev.next = removed.next;
    }


    private class NodeIterator implements Iterator<Integer> {
        private Node p = head;

        @Override
        public boolean hasNext() { // 是否有下个元素
            return p != null;
        }

        @Override
        public Integer next() { // 返回当前值，并移动指针
            int value = p.value;
            p = p.next;
            return value;
        }
    }

    /**
     * 节点类
     */
    private static class Node {
        int value; // 值
        Node next; // 下一个节点

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    /**
     * 向链表头部添加元素
     * 头插法
     *
     * @param value 待添加值
     */
    public void addFirst(int value) {
        // 链表为空
//        head = new Node(value, null);
        // 链表非空
        head = new Node(value, head);
    }

    /**
     * 向链表尾部添加元素
     * 尾插法
     *
     * @param value 待添加值
     */
    public void addLast(int value) {
        Node last = findLast();
        if (last == null) {
            addFirst(value);
        } else {
            last.next = new Node(value, null);
        }
    }


    // 迭代器遍历
    public void loop1(Consumer<Integer> consumer) {
        Node current = head;
        while (current != null) {
            consumer.accept(current.value);
            current = current.next;
        }
    }

    // 循环遍历
    public void loop2(Consumer<Integer> consumer) {
        for (Node current = head; current != null; current = current.next) {
            consumer.accept(current.value);
        }
    }

    public void loop3(Consumer<Integer> before, Consumer<Integer> afeter) {
        recursion(head,before,afeter);
    }

    private void recursion(Node cur,Consumer<Integer> before, Consumer<Integer> afeter) {
        if (cur.next != null) {
            before.accept(cur.value);
            recursion(cur.next,before,afeter);
            afeter.accept(cur.value);
        }
    }


    // 查找最后一个节点
    private Node findLast() {
        Node current = head;
        while (current != null && current.next != null) {
            current = current.next;
        }
        return current;
    }

    // 查找第 index 个节点
    private Node findNode(int index) {
        int i = 0;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    public void test() {
        int i = 0;
        for (Node p = head; p != null; p = p.next, i++) {
            System.out.println(p.value + " 索引:" + i);
        }
    }


}


